package com.letwal.cmcvnd.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import com.letwal.cmcvnd.ProjectApplication
import com.letwal.cmcvnd.ProjectConfig
import com.letwal.cmcvnd.data.*
import com.letwal.cmcvnd.extend.*


/** 本地缓存工具 */
object SpUtil {

    //sp key token
    val SP_GARLICLOAN_KEY_GARLICLOAN_TOKEN = "TOKEN"

    //sp key user id
    val SP_GARLICLOAN_KEY_GARLICLOAN_USER_GARLICLOAN_ID = "USER_ID"

    //sp key mobile
    val SP_GARLICLOAN_KEY_GARLICLOAN_MOBILE = "MOBILE"

    //sp key country
    val SP_GARLICLOAN_KEY_GARLICLOAN_COUNTRY = "COUNTRY"

    //sp key api path
    val SP_GARLICLOAN_KEY_GARLICLOAN_API_GARLICLOAN_PATH = "APIPATH"

    //sp key reinstall
    val SP_GARLICLOAN_KEY_GARLICLOAN_REINSTALL = "REINSTALL"

    /** 获取sp句柄 */
    fun getSp(): SharedPreferences {
        val sp = ProjectApplication.context.getSharedPreferences(
            ProjectConfig.GARLICLOAN_Product,
            Context.MODE_PRIVATE
        )
        return sp
    }

    @SuppressLint("ApplySharedPref")
    fun saveSpValue(strKey: String, strValue: String) {
        val sp = getSp().edit()
        sp.putString(strKey, strValue)
        sp.commit()
    }

    @SuppressLint("ApplySharedPref")
    fun saveSpValueBoolean(strKey: String, booValue: Boolean) {
        val sp = getSp().edit()
        sp.putBoolean(strKey, booValue)
        sp.commit()
    }

    /** 保存 token */
    @SuppressLint("ApplySharedPref")
    fun saveToken(gToken: String) {
        saveSpValue(SP_GARLICLOAN_KEY_GARLICLOAN_TOKEN, gToken)
    }

    /** 获取 token */
    @SuppressLint("ApplySharedPref")
    fun getToken(): String {
        return getSp().getString(SP_GARLICLOAN_KEY_GARLICLOAN_TOKEN, "").toString()
    }

    /** 保存 user id */
    @SuppressLint("ApplySharedPref")
    fun saveUserId(gUserId: String) {
        saveSpValue(SP_GARLICLOAN_KEY_GARLICLOAN_USER_GARLICLOAN_ID, gUserId)
    }

    /** 获取 user id */
    @SuppressLint("ApplySharedPref")
    fun getUserId(): String {
        return getSp().getString(SP_GARLICLOAN_KEY_GARLICLOAN_USER_GARLICLOAN_ID, "")
            .toString()
    }

    /** 保存 mobile */
    @SuppressLint("ApplySharedPref")
    fun saveMobile(gMobile: String) {
        saveSpValue(SP_GARLICLOAN_KEY_GARLICLOAN_MOBILE, gMobile)
    }

    /** 获取 mobile */
    @SuppressLint("ApplySharedPref")
    fun getMobile(): String {
        return getSp().getString(SP_GARLICLOAN_KEY_GARLICLOAN_MOBILE, "")
            .toString()
    }

    /** 保存 country */
    @SuppressLint("ApplySharedPref")
    fun saveCountry(gCountry: String) {
        saveSpValue(
            SP_GARLICLOAN_KEY_GARLICLOAN_COUNTRY,
            gCountry
        )
    }

    /** 获取 country */
    @SuppressLint("ApplySharedPref")
    fun getCountry(): String {
        return getSp().getString(SP_GARLICLOAN_KEY_GARLICLOAN_COUNTRY, "")
            .toString()
    }

    /** 保存 api path */
    @SuppressLint("ApplySharedPref")
    fun saveApiPath(gApiPath: String) {
        saveSpValue(
            SP_GARLICLOAN_KEY_GARLICLOAN_API_GARLICLOAN_PATH,
            gApiPath
        )
    }

    /** 获取 api path */
    @SuppressLint("ApplySharedPref")
    fun getApiPath(): String {
        return getSp().getString(SP_GARLICLOAN_KEY_GARLICLOAN_API_GARLICLOAN_PATH, "")
            .toString()
    }


    /** 保存 reinstall */
    @SuppressLint("ApplySharedPref")
    fun saveReInstall(reinstall: Boolean) {
        saveSpValueBoolean(
            SP_GARLICLOAN_KEY_GARLICLOAN_REINSTALL,
            reinstall
        )
    }

    /** 获取 reinstall */
    @SuppressLint("ApplySharedPref")
    fun getReInstall(): Boolean {
        return getSp().getBoolean(SP_GARLICLOAN_KEY_GARLICLOAN_REINSTALL, true)
    }
}